<?php


namespace App\Http\Controllers\Saas;


use App\Contract\ContractStatus;
use App\Http\Controllers\Controller;
use App\Http\Resources\PaginationCollection;
use App\Http\Service\DeviceService;
use App\Jobs\CheckAuto;
use App\Model\Contract;
use App\Model\ContractTenant;
use App\Model\Country;
use App\Model\Device;
use App\Model\House;
use App\Model\RestartDevice;
use App\Model\RestartLog;
use App\Utils\Result;
use App\Utils\Token;
use Illuminate\Http\Request;

class RestartDeviceController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $query = RestartDevice::query();
        $page = $request->input('page', 1);
        $pageSize = $request->input('pageSize', 10);
        $sn = $request->input('sn');
        $query->when($sn, function($q) use ($sn) {
            $q->where('sn', 'like', "%{$sn}%");
        });
        $data = $query->latest()->paginate($pageSize, ['*'], 'page', $page);
        return Result::success(new PaginationCollection($data));
    }

    public function show($id)
    {
        $sp = RestartDevice::query()->where('id',$id)->first(['id','sn']);
        return Result::success($sp);
    }

    public function add(Request $request)
    {
        $data = $request->only([
            'sn',
        ]);
        if(!$data['sn']){
            return Result::error(401, '请输入设备号');
        }
        $device = Device::query()->where('sn',$data['sn'])->first();
        if(!$device){
            return Result::error(401, '设备号不存在');
        }
        $check = RestartDevice::query()->where('sn',$data['sn'])
        ->first();
        if($check){
            return Result::error(401, '已存在 请勿重复添加');
        }
        $sp = new RestartDevice();
        $sp->fill($data);
        $sp->save();
        return Result::success();
    }

    public function edit(Request $request, $id)
    {
        $data = $request->only([
            'sn',
        ]);
        if(!$data['sn']){
            return Result::error(401, '请输入设备号');
        }
        $device = Device::query()->where('sn',$data['sn'])->first();
        if(!$device){
            return Result::error(401, '设备号不存在');
        }
        $check = RestartDevice::query()->where('sn',$data['sn'])->where('id','<>',$id)->first();
        if($check){
            return Result::error(401, '已存在 请勿重复添加');
        }
        $sp = RestartDevice::find($id);
        $sp->fill($data);
        $sp->save();
        return Result::success();
    }

    /**删除
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function del($id){
        $sp = RestartDevice::find($id);
        $sp->delete();
        return Result::success();
    }

    /**自动重启
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function autoRestart(){
        $list = RestartDevice::query()->get();
        $v = 1;
        foreach ($list as $item) {

            $deviceModel = Device::where('sn', $item['sn'])->first();
            if ($deviceModel) {
                $v+=1;
                dispatch(new CheckAuto($deviceModel))->delay(6*$v);
            }
        }
        return Result::success();
    }

    public function restartLog(Request $request){
        $query = RestartLog::query();
        $page = $request->input('page', 1);
        $pageSize = $request->input('pageSize', 10);
        $sn = $request->input('sn');
        $query->when($sn, function($q) use ($sn) {
            $q->where('sn', 'like', "%{$sn}%");
        });
        $data = $query->latest()->paginate($pageSize, ['*'], 'page', $page);
        return Result::success(new PaginationCollection($data));
    }
}
